<?php

/*
 * Action - Attack - Knight 5$
 *
 * Shuffle the Knight pile before each game with it. Keep it face down except for the top card, which is the only one that can be bought or gained.
 */

class Knights extends Card implements iAction, iKingdom, iDarkAges {

    public function __construct() {
        parent::__construct("Knights", 5);
        $this->undoPlay = false;
        $this->undoBuy = false;
    }

    public function getDescription() {
        return array(
            "Shuffle the Knight pile before each game with it. Keep it face down except for the top card, which is the only one that can be bought or gained.");
    }

    public function play(Game $game, $isRealCard) {
        
    }
    
    public function knightAttack(Game $game, $attacking_knight) {
        
        $game->setPhase(Phase_Sleep);
        $names = $game->getOtherPlayersNames(true);
        $trashThisKnigth = false;
        foreach ($names as $name) {
            $cards = $game->removeCardsFromDrawDeck(2, $name);

            ScreenDisplay::addScreenMessage($game, true, " reveal " . nicePrint($cards), " reveals " . ScreenDisplay::$LMSG, $name);

            $has_trashable_cards = false;
            $trashable_cards = array();
            foreach ($cards as $card) {
                $coins = $card->getPrice($game)->getCoins();
                if ($coins >= 3 && $coins <= 6) {
                    $has_trashable_cards = true;
                    $trashable_cards[] = $card;
                } else {
                    $game->putCardInDiscardDeck($card, $name);
                }
            }

            $trashed_card = null;
            //remember that the other player treasure cards are currently in the 'air' we should decide where to put them
            if (sizeof($trashable_cards) == 2) {

                $output = MySocket::GetUserReply($game, Input_ChooseOne, $trashable_cards, 1, 1, null, "Choose a card to trash", $name);


                $trashed_card = $output[0];
                $trashed_card = n2c($trashed_card);

                $card1_name = $trashable_cards[0];
                $card2_name = $trashable_cards[1];
                $other_card = null;
                if ($card1_name->getKey() == $trashed_card->getKey()) {
                    $other_card = $card2_name;
                } else if ($card2_name->getKey() == $trashed_card->getKey()) {
                    $other_card = $card1_name;
                }
                //we discard the not chosen card
                $game->putCardInDiscardDeck($other_card, $name);
            } else if (sizeof($trashable_cards) == 1) {
                $trashed_card = $trashable_cards[0];
            }

            if ($trashed_card) {
                //trashing the coosen card
                $game->putCardInTrash($trashed_card);
                ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashed_card), " trashes " . ScreenDisplay::$LMSG, $name);
            }
            
            if ($trashed_card instanceof iKnight) {
                $trashThisKnigth = true;
            }
        }
        
        if ($trashThisKnigth) {
            $trashed_knight = $game->moveCardFromPileToPile($attacking_knight, PlayArea, Trash);
            ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashed_knight), "trashes " . ScreenDisplay::$LMSG);
        }
    }

    public function boardSetup(Game $game) {
        $game->addKnights();
        return true;
    }
	
	public function isActualCard() {
		return false;
	}
}

?>